﻿using DotNet.Kernel;
using DotNet.Utilities;
using BPMS.Entity;
using System.Collections;
using System.Text;
using BPMS.Service;
using System.Data;

namespace BPMS.Service
{
    /// <summary>
    /// 通讯录
    /// <author>
    ///		<name>MaoQingyu</name>
    ///		<date>2014.7.11</date>
    /// </author>
    /// </summary>
    public class BPMS_PersonDAL
    {
        #region Method
        /// <summary>
        /// 获取最大编号
        /// </summary>
        /// <returns></returns>
        public object GetMaxCode()
        {
            return DataFactory.DbUtils().GetMax("BPMS_Person", "PerId");
        }
        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="KeyValue">主键值</param>
        /// <returns></returns>
        public int GetRecordCount(string KeyValue)
        {
            return DataFactory.DbUtils().RecordCount("BPMS_Person", "PerId", KeyValue);
        }
        /// <summary>
        /// 获取记录总数（带条件）
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="param">参数化</param>
        /// <returns></returns>
        public int GetRecordCount(StringBuilder where, SqlParam[] param)
        {
            return DataFactory.DbUtils().RecordCount("BPMS_Person", where, param);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public int Insert(BPMS_Person entity)
        {
            int IsOk = DataFactory.DbUtils().Insert(entity);
            #region 写日操作日志
            if (IsOk > 0)
            {
                BPMS_SysLogDAL.Instance.AddTaskLog<BPMS_Person>(entity, SessionHelper.GetSessionUser().UserId, SessionHelper.GetSessionUser().UserName);
            }
            #endregion
            return IsOk;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public int Update(BPMS_Person entity)
        {
            #region 获取旧值
            var oldEntity = this.GetEntity(BPMS_SysLogDAL.Instance.GetKeyFieldValue<BPMS_Person>(entity).ToString());
            #endregion
            int IsOk = DataFactory.DbUtils().Update(entity, "PerId");
            #region 写日操作日志
            if (IsOk > 0)
            {
                BPMS_SysLogDAL.Instance.UpdateTaskLog<BPMS_Person>(oldEntity, entity, SessionHelper.GetSessionUser().UserId, SessionHelper.GetSessionUser().UserName);
            }
            #endregion
            return IsOk;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="KeyValue">主键</param>
        /// <returns></returns>
        public int Delete(string KeyValue)
        {
            #region 获取旧值
            var oldEntity = this.GetEntity(KeyValue);
            #endregion
            int IsOk = DataFactory.DbUtils().Delete("BPMS_Person", "PerId", KeyValue);
            #region 写日操作日志
            if (IsOk > 0)
            {
                BPMS_SysLogDAL.Instance.DeleteTaskLog<BPMS_Person>(oldEntity, SessionHelper.GetSessionUser().UserId, SessionHelper.GetSessionUser().UserName);
            }
            #endregion
            return IsOk;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="KeyValue">主键</param>
        /// <returns></returns>
        public int BatchDelete(string[] KeyValue)
        {
            return DataFactory.DbUtils().BatchDelete("BPMS_Person", "PerId", KeyValue);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="KeyValue">主键</param>
        /// <returns></returns>
        public BPMS_Person GetEntity(string KeyValue)
        {
            return DataFactory.DbUtils().GetModelById<BPMS_Person>("PerId", KeyValue);
        }
        /// <summary>
        /// 分页获取数据列表(带条件)
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="param">参数化</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="orderType">排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">总条数</param>
        /// <returns></returns>
        public IList GetPageListWhere(StringBuilder where, SqlParam[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT * FROM BPMS_Person WHERE 1=1");
            strSql.Append(DataPermission.Instance.Person);
            strSql.Append(where);
            return DataFactory.SqlHelper().GetPageList<BPMS_Person>(strSql.ToString(), param, CommonHelper.ToOrderField("SortCode", orderField), orderType, pageIndex, pageSize, ref count);
        }
        /// <summary>
        /// 分页获取数据列表(带条件)
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="param">参数化</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="orderType">排序类型</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="count">总条数</param>
        /// <returns></returns>
        public DataTable GetPageListWhere2(StringBuilder where, SqlParam[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT
                                    P.PerId,
                                    P.RealName,
                                    ORG0.FullName AS SubCompanyId,
                                    ORG1.FullName AS CompanyId,
                                    ORG2.FullName AS DepartmentId,
                                    ORG3.FullName AS WorkgroupId,
                                    P.Tel1,
                                    P.Tel2,
                                    P.Tel3,
                                    P.Sex,
                                    P.ModifyDate 
                                FROM BPMS_Person P 
                                    LEFT JOIN BPMS_Organization ORG0 ON ORG0.OrganizationId = P.SubCompanyId 
                                    LEFT JOIN BPMS_Organization ORG1 ON ORG1.OrganizationId = P.CompanyId 
                                    LEFT JOIN BPMS_Organization ORG2 ON ORG2.OrganizationId = P.DepartmentId 
                                    LEFT JOIN BPMS_Organization ORG3 ON ORG3.OrganizationId = P.WorkgroupId where 1=1");
            strSql.Append(where);
            return DataFactory.SqlHelper().GetPageList(strSql.ToString(), param, CommonHelper.ToOrderField("SortCode", orderField), orderType, pageIndex, pageSize, ref count);
        }

        #endregion

        /// <summary>
        /// 自动补全(显示20行)
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="param">参数化</param>
        /// <returns></returns>
        public IList autocomplete(StringBuilder where, SqlParam[] param)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT TOP 10 PerId,RealName FROM BPMS_Person WHERE 1=1");
            strSql.Append(where);
            strSql.Append(DataPermission.Instance.Person);
            strSql.Append(" AND Enabled = 1");
            strSql.Append(" ORDER BY CreateDate");
            return DataFactory.SqlHelper().GetDataListBySQL<BPMS_Person>(strSql, param);
        }
    }
}
